import tkinter as tk
from tkinter import ttk
from typing import Tuple, Dict, Optional, Callable
from title_bar import CustomTitleBar

class CustomWindow(tk.Tk):
    """自定义窗口主类，管理整体布局和页面切换"""
    
    def __init__(self, title: str = "自定义窗口", 
                 size: Tuple[int, int] = (500, 300),
                 position: Tuple[int, int] = (300, 200),
                 title_bar_config: Optional[Dict] = None,
                 **kwargs):
        super().__init__(** kwargs)
        
        # 基础窗口配置
        self.title(title)
        self.geometry(f"{size[0]}x{size[1]}+{position[0]}+{position[1]}")
        
        # 移除系统标题栏
        self.overrideredirect(True)
        
        # 配置全局样式
        self._configure_styles()
        
        # 创建主容器
        self.main_container = tk.Frame(self, bg="#f0f0f0")
        self.main_container.pack(fill=tk.BOTH, expand=True)
        
        # 创建标题栏
        title_bar_defaults = {
            "title": title,
            "bg_color": "#2c3e50",
            "text_color": "white",
            "height": 30
        }
        if title_bar_config:
            title_bar_defaults.update(title_bar_config)
            
        # self.title_bar = CustomTitleBar(self.main_container, **title_bar_defaults)
        self.title_bar = CustomTitleBar(self, **title_bar_defaults)
        
        # 创建内容区域容器
        self.content_frame = tk.Frame(self.main_container, bg="white")
        self.content_frame.pack(fill=tk.BOTH, expand=True)
        
        # 存储所有页面
        self.pages = {}
        
    def _configure_styles(self) -> None:
        """配置ttk组件样式"""
        self.style = ttk.Style()
        self.style.theme_use('clam')
        
        # 可以在这里定义全局样式
        self.style.configure("TButton", font=("Arial", 10))
        self.style.configure("TLabel", font=("Arial", 10))
    
    def add_page(self, page_name: str, frame_class) -> tk.Frame:
        """
        添加页面到窗口
        
        :param page_name: 页面名称，用于标识和切换
        :param frame_class: 页面类（继承自BasePage）
        :return: 创建的页面实例
        """
        page = frame_class(self.content_frame)
        self.pages[page_name] = page
        page.pack(fill=tk.BOTH, expand=True)
        page.hide()  # 默认隐藏新页面
        return page
    
    def show_page(self, page_name: str) -> None:
        """
        显示指定页面，隐藏其他所有页面
        
        :param page_name: 要显示的页面名称
        """
        for page in self.pages.values():
            page.hide()
        
        if page_name in self.pages:
            self.pages[page_name].show()
    
    def add_title_bar_button(self, name: str, text: str, bg_color: str, 
                            command: Callable, fg_color: str = "white") -> None:
        """
        向标题栏添加按钮
        
        :param name: 按钮名称
        :param text: 按钮显示文本
        :param bg_color: 按钮背景色
        :param command: 点击事件回调
        :param fg_color: 按钮前景色
        """
        self.title_bar.add_button(name, text, bg_color, command, fg_color)
    
    def set_title(self, title: str) -> None:
        """更新窗口标题"""
        self.title_bar.set_title(title)
